package com.caseProcess.service.impl;

import com.caseProcess.entity.CaseOperNote;
import com.caseProcess.entity.CaseProcInfo;
import com.caseProcess.entity.Resources;
import com.caseProcess.mapper.CaseOperNoteDao;
import com.caseProcess.mapper.ResourcesDao;
import com.caseProcess.service.CaseOperNoteService;
import com.caseProcess.service.CaseProcInfoService;
import com.caseProcess.service.ResourcesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CaseOperNoteServiceImpl implements CaseOperNoteService {
   @Resource
   private CaseOperNoteDao caseOperNoteDao;

    @Resource
    private CaseProcInfoService caseProcInfoService;

    @Resource
    private ResourcesService resourcesService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(CaseOperNote caseOperNote) throws Exception {
        CaseProcInfo caseProcInfo=new CaseProcInfo();
        caseProcInfo.setProcTatus(caseOperNote.getProcessStatus().getStaticId());
        caseProcInfo.setProcessId(caseOperNote.getCaseProcInfo().getProcessId());
        if(caseOperNote.getProcessStatus().getStaticId()==1&&!"0".equals(caseOperNote.getParentId())){//回退 同时修改为回退状态
            caseProcInfo.setBack(true);
        }
        if("0".equals(caseOperNote.getParentId())){
            caseProcInfo.setProcUser(caseOperNote.getNextUser());
        }
        try {
            caseOperNoteDao.insert(caseOperNote);//添加操作过程
            caseProcInfoService.update(caseProcInfo);//修改分配过程
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertAndFile(CaseOperNote caseOperNote) throws Exception {
        CaseProcInfo caseProcInfo=new CaseProcInfo();
        caseProcInfo.setProcTatus(caseOperNote.getProcessStatus().getStaticId());
        caseProcInfo.setProcessId(caseOperNote.getCaseProcInfo().getProcessId());
        Resources resources=new Resources();
        resources.setCaseOperNote(caseOperNote);
        try {
            int upSize=resourcesService.updateByOperNoteId(caseOperNote.getCaseProcInfo().getProcessId(),caseOperNote.getOperNoteId());//完善文件上传
            if(upSize>0){
                caseOperNote.setResources(true);
            }
            caseOperNoteDao.insert(caseOperNote);//添加操作过程
            caseProcInfoService.update(caseProcInfo);//修改分配过程

        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertAndFileTwo(CaseOperNote caseOperNote) throws Exception {
        CaseProcInfo caseProcInfo=new CaseProcInfo();
        caseProcInfo.setProcTatus(caseOperNote.getProcessStatus().getStaticId());
        caseProcInfo.setProcessId(caseOperNote.getCaseProcInfo().getProcessId());
        Resources resources=new Resources();
        resources.setCaseOperNote(caseOperNote);
        try {
            int upSize=resourcesService.updateByOperNoteId(caseOperNote.getCaseProcInfo().getProcessId(),caseOperNote.getOperNoteId());//完善文件上传
            if(upSize>0){
                caseOperNote.setResources(true);
            }
            caseOperNoteDao.insert(caseOperNote);//添加操作过程
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return true;
    }

    @Override
    public int update(CaseOperNote caseOperNote) {
        return caseOperNoteDao.update(caseOperNote);
    }

    @Override
    public int delete(CaseOperNote caseOperNote) {
        return caseOperNoteDao.delete(caseOperNote);
    }

    @Override
    public List<CaseOperNote> selectCaseOperNotes(CaseOperNote caseOperNote) {
        return caseOperNoteDao.selectCaseOperNotes(caseOperNote);
    }
}
